<?php
/*
* Author: Nguyen Hai Nam
* Module: Admin
* Desc	: Introduce pick.vn and sidle pic
* Date	: 12/12/2011
*/
class UserModule extends Pick_Core_Module{
	
	public $authentication = 1;
	
	function UserModule(){
		
		parent::Pick_Core_Module();
		$this->theme = "admin";
		
		# Languages
		$languages = new Pick_Core_Languages();
		$objectLanguages = "";
		$objectLanguages = $languages->getObjects(1, 'status = 1', array('position'=>'ASC'), '');
		if($objectLanguages){
			$objectLanguages = $objectLanguages['data'];	
		}#endif
		$this->template->assign("objectLanguages", $objectLanguages);	
        
        // Get Path for this module
        $this->template->assign('arrayParentPath', $this->getArrayParentPath());
	}
	public function getContent(){
		
		$act = $this->request->element('act', "list");
		$this->template->assign('act', $act);
		if($act == "add"){
			return $this->addAction();	
		}else{
			return $this->listAction();	
		}
		
	}
	public function listAction(){
		
		$usertypes 		= new Pick_Core_UserTypes();
		$users 			= new Pick_Core_User();
		$actions 		= new Pick_Core_Actions();
		$actions->setInfoAction($users, $this->request);
		
		$items_per_pages = DEFAULT_ADMIN_ROWS_PER_PAGE;
		$infoText = '';
		$infoClass = 'infoText';
		
		# Read parameters from request
		$searchTerms 	= $this->request->element('searchTerms','');
		$searchStatus 	= $this->request->element('searchStatus','-1');
		$searchUserType = $this->request->element('searchUserType','-1');
		$page 			= $this->request->element('page',1);
		$pid 			= $this->request->element('pid',0);
		$orderBy 		= $this->request->element('orderBy','id');
		$orderDir 		= $this->request->element('orderDir','ASC');

		$objectMenu = $usertypes->getObjects(1, '1=1', array('position'=>'ASC'),'');
		if($objectMenu){
			$objectMenu = $objectMenu['data'];
		}
		
		# Lay pid mac dinh
		if(!$pid){
			if(is_array($objectMenu)){
				$pid = $objectMenu[0]->getField('id');
			}
		}
		
		# Menu
		$menu = $this->getMenu();
		
		# Control plus action
		$plus 			= $this->request->element('plus','');
		if($plus){
			$actions->auto($plus);
			$infoText 	= $actions->getInfoAction();
			$infoClass 	= "errorText";
		}
		
		# Build search condition
		$searchCond = "type = " . $pid;
		if($searchStatus != '-1') $searchCond .= " AND status = '$searchStatus'";
		if($searchTerms != '') $searchCond .= " AND (username LIKE '%".$searchTerms."%' OR name LIKE '%".$searchTerms."%' OR id = '".$searchTerms."')";
		
		# Get all categories object and pass to template
		$listObjects = $users->getObjects($page, $searchCond, array($orderBy => $orderDir), $items_per_pages);
		
		# Generate status combobox for Search form
		$listStatus = optionStatus($searchStatus);		
		
		# Pass some variables to template
		$this->template->assign('pid',$pid);
		$this->template->assign('page',$page);
		$this->template->assign('listStatus',$listStatus);
		$this->template->assign('listObjects',$listObjects);
		$this->template->assign('searchTerms',$searchTerms);
		$this->template->assign('searchStatus',$searchStatus);
		$this->template->assign('orderBy',$orderBy);
		$this->template->assign('orderDir',$orderDir);
		$this->template->assign('infoText',$infoText);
		$this->template->assign('infoClass',$infoClass);
		$this->template->assign('menu',$menu);

	}
	
	public function addAction(){
		
		$usertypes 		= new Pick_Core_UserTypes();
		$users 			= new Pick_Core_User();
		$objectInfo     = new Pick_Core_UserInfo();
		$modules 		= new Pick_Core_Modules();
		
		$infoText  = '';
		$infoClass = '';
		$pid = $this->request->element('pid', '');
		$oId 	      = $this->request->element('oId','0');
		$plus 	      = $this->request->element('plus','');
		
		$objectMenu = $usertypes->getObjects(1, '1=1', array('position'=>'ASC'),'');
		if($objectMenu){
			$objectMenu = $objectMenu['data'];
		}
		$this->template->assign("objectMenu", $objectMenu);	
		
		# Lay menu mac dinh
		if(!$pid){
			if(is_array($objectMenu)){
				$pid = $objectMenu[0]->getField('id');
			}
		}
		
		# Menu
		$menu = $this->getMenu();
		
		# Read parameters from request
		$arrayFieldFromMySQL = $users->getListFields();
		$object = $users->getObject($oId);
		if(is_object($object)){
			foreach($arrayFieldFromMySQL as $fieldName){
				$fields[$fieldName] = $this->request->element($fieldName, $object->getField($fieldName));
			}#endforeach		
		}else{
			$oId = 0;// Fix truong hop mau tin bi xoa khoi csdl ma trinh duyet van du id cua mau tin do
			foreach($arrayFieldFromMySQL as $fieldName){
				$fields[$fieldName] = $this->request->element($fieldName,'');
			}#endforeach
		}
		
		
		# Modify item of array
		if(is_object($object)){
			$module = $this->request->element('module', unserialize($object->getField('permissions')));    
		}else{
			$module = $this->request->element('module','');   
		}
		$this->template->assign("module", $module);
		$confirm 	= $this->request->element('confirm','');
		$this->template->assign("confirm", $confirm);
		$fields['permissions'] = serialize($module);
		
		# Put data into object
		$objectInfo->setArrayField($fields);
		$this->template->assign("objectInfo", $objectInfo);
		
		
		# Truy du lieu qua template
		$objectModules = $modules->getObjects(1, 'status = 1 AND typeid = 1', array('position'=>'ASC'), '');
		$this->template->assign("objectModules",$objectModules);
		
		$defaultType = DEFAULT_LANGUAGE."_type";
		$listStatus = optionStatus($objectInfo->getField('status'), DEFAULT_ADMIN_LANGUAGE);
		$this->template->assign("listStatus", $listStatus);
		$listUserType = $usertypes->comboListObjects('id', $defaultType, $objectInfo->getField('type'));
		$this->template->assign("listUserType", $listUserType);
			
		foreach($arrayFieldFromMySQL as $fieldName){
			$this->template->assign($fieldName, $objectInfo->getField($fieldName));
		}#endforeach	
		
		
		if($_POST && $plus == 'saveItem') {
			# Validation
			$validate 	= new Pick_Data_Validate();
			$validatemess = $this->validateData($this->request, $validate);
			
			if($validatemess == '') {
				/* Test if it's exists then can't add theme more'' */
				$object = "";
				
				if($oId<=0){
					/* Kiểm tra mẫu tin nếu chưa có thì chèn vào csdl */
					$object = $users->getObjectFromUser($objectInfo->getField('username'));
				}
		
				if(is_object($object)){
					$infoText = $this->l('item_inserted_error');	
					$infoClass = "errorText";
				}else{
					if(!empty($fields['password'])){
						$objectInfo->setField("password", md5($fields['password']));
					} else{
						unset($fields['password']);
					}
					$ok = $users->updateData($objectInfo->getArrayField(),$oId);
					$infoText = $validatemess;
					if($ok){
						$infoText = $this->l('item_save_ok');
						$infoClass = "InfoText";
						$oId = $oId?$oId:mysql_insert_id();			
					}
				}
				
			}else {
				$infoText = $this->l('error')."<br />".$validatemess;
				$infoClass = "errorText";
			}
		}
		$this->template->assign("oId",$oId);
		$this->template->assign("infoText",$infoText);
		$this->template->assign("infoClass",$infoClass);	
		$this->template->assign("menu",$menu);	

	}		
	public function getMenu(){
		
		$pid = $this->request->element('pid', '');
		$usertypes = new Pick_Core_UserTypes();
		$objectMenu = $usertypes->getObjects(1, '1=1', array('position'=>'ASC'),'');
		if($objectMenu){
			$objectMenu = $objectMenu['data'];
		}
		$this->template->assign("objectMenu", $objectMenu);	
		
		# Lay menu mac dinh
		if(!$pid){
			if(is_array($objectMenu)){
				$pid = $objectMenu[0]->getField('id');
			}
		}
		$this->template->assign("pid", $pid);
		
		return $this->displayPart('user/menu');	
	}
	function validateData($request, $validate) {
		$infoText = '';
		$infoText .= $validate->validStatus($this->l('status'), $request->element('status'));
		$infoText .= $validate->validStatus($this->l('user_type'), $request->element('type'));
		$infoText .= $validate->validUsername($this->l('username'), $request->element('username'));
		//$infoText .= $validate->validPassword($messages['password'], $request->element('password'));
		$infoText .= $validate->validTestPass($this->l('testpass'), $request->element('password'), $request->element('confirm'));
		return $infoText;
	}	
}